package com.xingkeduo.business.operation.controller.api;

import static com.xingkeduo.tool.OperaConstants.API_URL_PREFIX;
import java.util.List;
import java.util.Map;

import com.xingkeduo.pojo.dto.Shops.shopsApi.ShopsScreenInfoDTO;
import com.xingkeduo.pojo.dto.Shops.shopsApi.ShopsStatusApiDTO;
import com.xingkeduo.pojo.vo.SeeShopIdVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.controller.InitController;
import com.xingkeduo.business.operation.domain.ShopsDomain;
import com.xingkeduo.business.operation.json.BaseDTOJTO;
import com.xingkeduo.pojo.dto.Shops.shopsApi.ShopsApiDTO;
import com.xingkeduo.pojo.dto.Shops.shopsApi.ShopsGrainApiDTO;
import com.xingkeduo.pojo.json.ShopsJson;
import com.xingkeduo.pojo.query.ShopsQuery;
import com.xingkeduo.pojo.table.ShopsTable;
import com.xingkeduo.pojo.vo.ServprodVo;
import com.xingkeduo.tool.ResponseUtils;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.pojo.exception.SystemException;
import com.xingkeduo.util.tool.ReturnUtil;

/**
 * 门店管理
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Slf4j
@RestController
@RequestMapping(API_URL_PREFIX +"/shops")
public class ShopsApiController extends InitController {

    @Autowired
    private ShopsDomain shopsDomain;
    /**
     * API获取门店详情，调取发型师接口
     * @param id
     * @param craftsmanid
     * @return
     */
    @GetMapping(value = "/getShopsDetale")
    public ResponseUtils doGet(Long id , String craftsmanid) {
        log.info("查看门店详情" + id);
        returnUtil = new ReturnUtil();
        try {
            ShopsApiDTO shops = shopsDomain.getShopsApiById(id ,craftsmanid);
            return ResponseUtils.getSuccess(shops);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }
    /**
     * 获取门店详情
     * @return
     * @throws Throwable
     */
    @GetMapping(value = "/getShopsDetales")
    public ResponseUtils doGets(Long id) {
        returnUtil = new ReturnUtil();
        try {
            ShopsApiDTO shops = shopsDomain.getShopsApiByIds(id);
            return ResponseUtils.getSuccess(shops);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }

    //列表
    @RequestMapping(value = "/lists/{urlParam:.+}", method = RequestMethod.GET)
    public ResponseUtils doGetLists(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            ShopsQuery shopsQuery = new ShopsQuery();

            shopsQuery=  JSON.parseObject(urlParam,ShopsQuery.class);

            List<ShopsTable> shopsList= shopsDomain.getShopsApiList(shopsQuery);

            return ResponseUtils.getSuccess(shopsList);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }

    /**
     * APP店铺列表(根据店铺ID和状态查询店铺列表列表)
     *  api/op/shops/getShopsList?sign=1&statusList=1,2
     */

    @GetMapping(value = "/getShopsList")
//    @RequestMapping("/getShopsList")
    public ResponseUtils doGetShopsList(ShopsQuery shopsQuery) {
        returnUtil = new ReturnUtil();
        try {
            List<ShopsTable> shops = shopsDomain.doGetShopsList(shopsQuery);
            return ResponseUtils.getSuccess(shops);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }

    /**
     *app返回1.预热中2.开业中 3.临时闭店的状态列表，不返回 0.已创建和 4.已撤店
     */
    @GetMapping(value = "/getShopsStatusList")
    public ResponseUtils dogetStatus(ShopsQuery shopsQuery) {
        returnUtil = new ReturnUtil();
        try {
            List<ShopsStatusApiDTO> shops = shopsDomain.doGetShopsStatusList(shopsQuery);
            return ResponseUtils.getSuccess(shops);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }

    /**
     * 获取门店各sku最低价
     * @return
     * @throws Throwable
     */
    @GetMapping(value = "/getStoreSkuInfoListByStoreId")
    public ResponseUtils getStoreSkuInfoListByStoreId(Long storeId) {
        returnUtil = new ReturnUtil();
        try {
        	List<ServprodVo> info = shopsDomain.getStoreSkuInfoListByStoreId(storeId);
            return ResponseUtils.getSuccess(info);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }



    /**
     * 小程序店铺列表 pantong
     * @param f_user_id
     * @param longitude
     * @param latitude
     * @param status
     * @param offset
     * @param is_first
     * @param mobile_type
     * @return
     * @throws Throwable
     */
    @GetMapping("/list_transfer")
    public Object list(Long f_user_id, Double longitude, Double latitude, Long status, int offset, int is_first, String mobile_type) throws Throwable{
    	
    	try {
    		Map<String,Object> result = shopsDomain.getShopsList(f_user_id, longitude, latitude, status, offset, is_first, mobile_type);
//    		result.put("upgrade_system", true);//上线前改回去 改成true
//    		if(status!=null && status.longValue() == 383L) {
//    			result.put("upgrade_system", false);
//    		}
    		return BaseDTOJTO.toSuccessJson(ShopsJson.toJsonForShopsList(result));
    	}catch (DataException e) {
    		dealException(e);
			return BaseDTOJTO.toErrorJson(e.getMessage());
    	}catch (Exception e) {
    		dealException(e);
    		return BaseDTOJTO.toErrorJson("获取失败，请稍后重试！");
		}
    }
    
    /**
     * 小程序店铺详情 pantong
     * @param f_user_id
     * @param longitude
     * @param latitude
     * @param mobile_type
     * @return
     * @throws Throwable
     */
    @GetMapping("/detail_transfer")
    public Object detail_transfer(long f_user_id, Double longitude, Double latitude, Long store_id, String  time, String token, String mobile_type) throws Throwable{
    	
    	try {
    		Map<String,Object> result = shopsDomain.getShopsDetail(f_user_id, longitude, latitude, store_id, time, token, mobile_type);
    		return BaseDTOJTO.toSuccessJson(ShopsJson.toJsonForShopDetail(result));
    	}catch (DataException e) {
    		dealException(e);
			return BaseDTOJTO.toErrorJson(e.getMessage());
    	}catch (Exception e) {
    		dealException(e);
    		return BaseDTOJTO.toErrorJson("获取失败，请稍后重试！");
		}
    }
    
    //APP库存颗粒
    @RequestMapping(value = "/grain/{urlParam:.+}", method = RequestMethod.GET)
    public String doGetGrain(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            ShopsQuery shopsQuery = ShopsJson.createQueryForShops(urlParam);
            long id = shopsQuery.getId();
            ShopsGrainApiDTO shopsGrainApiDTO = shopsDomain.getShopsGrain(id);
            return returnUtil.getSuccess(ShopsJson.toGranJson(shopsGrainApiDTO));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    /**
     * 下单屏幕店铺信息接口
     * @param id
     * @return
     */
    @GetMapping(value = "/getShopsScreenInfo")
    public ResponseUtils doShopsScreenInfo(Long id) {
        returnUtil = new ReturnUtil();
        try {
            ShopsScreenInfoDTO shops = shopsDomain.getShopsScreenInfo(id);
            return ResponseUtils.getSuccess(shops);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }


    @GetMapping(value = "/getCityId")
    public ResponseUtils doCityId(Long id) {
        returnUtil = new ReturnUtil();
        try {
           Long cityId = shopsDomain.getCityId(id);
            return ResponseUtils.getSuccess(cityId);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }

    /**
     * 根据店铺经纬度获取店铺ID、城市、城市名字
     * @param latitude
     * @param longitude
     * @return
     */
    @GetMapping(value = "/getLongitudeAndLatitudeShopInfo")
    public ResponseUtils doLongitudeAndLatitudeShopInfo(Double latitude , Double longitude ,Long userId) {
        log.info("根据经纬度获取门店ID-纬度" + latitude);
        log.info("根据经纬度获取门店ID-经度" + longitude);
        log.info("根据经纬度获取门店ID-用户ID" + userId);
        returnUtil = new ReturnUtil();
        try {
            SeeShopIdVo seeShopIdVo = shopsDomain.getLongitudeAndLatitudeShopInfo(latitude, longitude,userId);
            return ResponseUtils.getSuccess(seeShopIdVo);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }



}
